package com.cicc.springboot.controller;

import com.cicc.springboot.common.dto.R;
import com.cicc.springboot.model.MaterialQuery;
import com.cicc.springboot.service.impl.GenericExcelImportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ExcelImportController
 * @Description TODO
 * @Author 转轮王
 * @Date 2024/9/30 10:21
 * @Version 1.0
 */

@RestController

public class ExcelImportController {

    @Autowired
    private GenericExcelImportServiceImpl<MaterialQuery> excelImportService;


    /**
     * 导入物料表 Excel 文件
     */
    @PostMapping("/import")
    public R<String> importData(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("file = " + file.toString());
        return excelImportService.importExcelData(file);

    }

    /**
     * 导出 Excel 文件
     */
    @GetMapping("/export/excel")
    public R<String> exportExcel(@RequestParam("ids[]") List<Long> ids) {
        try {
            // 调用导出服务，传入前端传来的ID列表
            excelImportService.exportExcelData(ids);
            return R.ok("Excel 文件导出成功");
        } catch (IOException e) {
            return R.failed("导出失败，错误信息：" + e.getMessage());
        }
    }

    /**
     * 下载物料模板
     */
    @GetMapping("/export/template")
    public ResponseEntity<byte[]> exportTemplate() throws IOException {
        // 调用服务层方法，生成 Excel 模板字节数组
        byte[] bytes = excelImportService.generateExcelTemplate();

        // 设置响应头和返回内容
        HttpHeaders headers = new HttpHeaders();
        String fileName = URLEncoder.encode("物料表模板_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), "UTF-8").replaceAll("\\+", "%20");
        headers.setContentDisposition(ContentDisposition.attachment().filename(fileName + ".xlsx").build());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }

    /**
     * 导入采购订大表Excel文件
     */
    @PostMapping("/importOrder")
    public R<String> importExcel(@RequestParam("file") MultipartFile file) {
        return excelImportService.importExcel(file);

    }


    /**
     * 订单的模板导出
     */
    @GetMapping("/exportOrderTemplate")
    public ResponseEntity<byte[]> exportOrderTemplate() throws IOException {
        // 调用服务层方法，生成 Excel 模板字节数组
        byte[] bytes = excelImportService.orderTemplate();

        // 设置响应头和返回内容
        HttpHeaders headers = new HttpHeaders();
        String fileName = URLEncoder.encode("订单信息_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), "UTF-8").replaceAll("\\+", "%20");
        headers.setContentDisposition(ContentDisposition.attachment().filename(fileName + ".xlsx").build());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }


//    @PostMapping("/importOrder")
//    public R<String> importOrder(@RequestParam("file") MultipartFile file) {
//        // 调用服务层的导入方法，并返回结果
//        return excelImportService.importOrders(file);
//    }


}
